/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:SysUserImpl.java
 * <p>
 * 类产生时间: 2012-5-23 下午4:29:36
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.hui.platform.system.exception.HuiException;
import com.hui.platform.system.idgenarater.IdGenaraterUtil;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.MD5Util;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.ISysUserDao;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysDutyDict;
import com.oceansoft.core.domain.SysPartTime;
import com.oceansoft.core.domain.SysRole;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.service.ISysUserService;

/**
 * 人员信息管理实现类
 * 
 * @author sunj
 * 
 */
@Service
@Transactional
public class SysUserServiceImpl implements ISysUserService {

	@Resource
	private ISysUserDao sysUserDao;

	public Pagination<SysUser> findByPage(int pageIndex, SysUser user, String deptId) {
		return sysUserDao.findByPage(deptId, pageIndex, user);
	}

	public int saveAddUser(SysUser user) {
		List<SysUser> users = sysUserDao.findByPoliceNum(user.getPoliceNum());
		if (users.size() > 0)
			return 1;
		//sysUserDao.saveAddUser(user);
		
		user.setDbId(IdGenaraterUtil.nextIntValue("TABLEPK",SysUser.class.getSimpleName()));
		user.setDelflag("0");
		try {
			sysUserDao.create(user);
		} catch (HuiException e) {
			e.printStackTrace();
			return 1;
		}
		
		return 0;
	}

	public int saveEditUser(SysUser user) {
		List<SysUser> users = sysUserDao.findByPoliceNum(user.getPoliceNum());
		if (users.size() > 0) {
			if (users.get(0).getDbId() != user.getDbId())
				return 1;
		}
		sysUserDao.editUser(user);
//		try {
//			sysUserDao.update(user);
//		} catch (HuiException e) {
//			e.printStackTrace();
//			return 0;
//		}
		
		
//		SQLUtil u = new SQLUtil();
//		list<Map> l =  u.list("select a.ajbh,b.aa from aaa", dsd)

		return 0;
	}

	public SysUser findByPoliceNum(String policeNum) {
		List<SysUser> users = sysUserDao.findByPoliceNum(policeNum);
		return users.get(0);
	}

	@Override
	public SysUser findUserByDbId(Long dbId) {
		return sysUserDao.findUserByDbId(dbId);
	}

	@Override
	public List<SysUser> findUserByDbIdAndName(Long dbId, String name, String deptId) {
		return sysUserDao.findUserByDbIdAndName(dbId, name, deptId);
	}

	@Override
	public List<SysUser> findUserByDept(String deptId) {
		return sysUserDao.findUserByDept(deptId);
	}

	@Override
	public Pagination<SysUser> findUserByPagination(String deptId, int pageIndex) {
		return sysUserDao.findUserByPagination(deptId, pageIndex);
	}

	@Override
	public Pagination<SysUser> findUserContact(String deptId, int pageIndex) {
		return sysUserDao.findUserContact(deptId, pageIndex);
	}

	@Override
	public Pagination<SysUser> findUserDelegate(String deptId, int pageIndex, String name, String manageDept) {
		return sysUserDao.findUserDelegate(deptId, pageIndex, name, manageDept);
	}

	@Override
	public Pagination<SysUser> findUserByCritirialPagination(Map<String, String> params, int pageIndex) {
		return sysUserDao.findUserByCritirialPagination(params, pageIndex);
	}

	@Override
	public int deleteUserByDbId(String policenum) {
		return sysUserDao.deleteUserByDbId(policenum);
	}

	@Override
	public SysUser findUserByPoliceNum(String policeNum) {
		return sysUserDao.findUserByPoliceNum(policeNum);
	}

	@Override
	public Message updatePassword(SysUser sessionUser, SysUser formVO) {
		Message msg = new Message(Constant.PWD_UPDATE_SUCC, Constant.RESULT_SUCC);
		SysUser dbuser = findUserByPoliceNum(sessionUser.getPoliceNum());
		String pwd0 = dbuser.getPassword();
		String pwd1 = formVO.getPassword();
		String pwd2 = formVO.getPassword_2();
		if (StringUtils.hasText(pwd1) && StringUtils.hasText(pwd2) && pwd1.equals(pwd2)) {
			try {
				if (!pwd0.equals(MD5Util.encoderByMd5(pwd1))) {
					msg.setBody(Constant.PWD_VERIFY_FAILED);
					msg.setStatus(Constant.RESULT_FAILD);
				}
			} catch (Exception e) {
				msg.setBody(Constant.PWD_UPDATE_FAILED);
				msg.setStatus(Constant.RESULT_FAILD);
				msg.setThrowable(e);
				e.printStackTrace();
			}
		} else {
			msg.setBody(Constant.PWD_ENDTERD_DIFFER);
			msg.setStatus(Constant.RESULT_FAILD);
		}
		return msg;
	}

	@Override
	public boolean updatePassword(SysUser user) {
		return 1 == sysUserDao.updatePassword(user);
	}

	@Override
	public int updateUserInfo(SysUser sysUser) {
		int count = sysUserDao.findCountByIDCard(sysUser.getIdCard(), sysUser.getPoliceNum());
		if (count > 0)
			return 0;
		sysUserDao.updateUserInfo(sysUser);
		return 1;
	}

	@Override
	public int updateUserTelInfo(SysUser sysUser) {
		return sysUserDao.updateUserTelInfo(sysUser);
	}

	@Override
	public List<SysUser> findUserInfoByPoliceNum(String policeNum) {
		return sysUserDao.findUserInfoByPoliceNum(policeNum);
	}

	@Override
	public List<SysDutyDict> findAllDutyInfo() {
		return sysUserDao.findAllDutyInfo();
	}

	/**
	 * 旧版人员调动
	 */
	@Override
	public void transferUser(String dutyName, String deptId, String dutyId, String policeNum) {
		sysUserDao.transferUser(dutyName, deptId, dutyId, policeNum);
	}


	@Override
	public void updatePartTime(String deptId, String policeNum, String partTimeDeptId, String partTimeDeptName,
			String dutyId, String dutyName) {
		sysUserDao.updatePartTime(deptId, policeNum, partTimeDeptId, partTimeDeptName, dutyId, dutyName);
	}

	@Override
	public List<SysPartTime> queryPartTimeInfoByPoliceNum(String policeNum) {
		return sysUserDao.queryPartTimeInfoByPoliceNum(policeNum);
	}

	/**
	 * 旧版部门人员调动
	 */
	@Override
	public void updateUserDept(Map<String, String> params) {
	}

	@Override
	public int addUser(String deptId, String policeNum, String name, String dutyId, String dutyName, String idCard,
			String tel_office, String tel_home, String tel_mobile, String tel_short, String tel_inter,
			String tel_outSide) {
		SysUser sysUser = sysUserDao.findUserByPoliceNum(policeNum);
		int count = sysUserDao.findCountByIDCard(idCard);
		if (sysUser != null || count > 0) {
			return 2;
		} else {
			// 默认排序序号在最后
			int orderNum = sysUserDao.findMaxOrderNum(deptId) + 1;
			return sysUserDao.addUser(deptId, policeNum, name, dutyId, dutyName, idCard, tel_office, tel_home,
					tel_mobile, tel_short, tel_inter, tel_outSide, orderNum);
		}
	}

	@Override
	public int checkUserLogin(String policenum, String password) {
		return sysUserDao.checkUserLogin(policenum, password);
	}

	@Override
	public List<SysUser> findUsersByAppId(String appId) {
		return sysUserDao.findUsersByAppId(appId);
	}

	@Override
	public int deletePartTimeInfo(String policenum, String dutyid) {
		return sysUserDao.deletePartTimeInfo(policenum, dutyid);
	}

	@Override
	public List<SysUser> findOnleUserInfoByPoliceNum(String policenum) {
		return sysUserDao.findOnleUserInfoByPoliceNum(policenum);
	}

	@Override
	public void syncUpdateUserInfo(List<SysUser> data, String appid) {

	}

	@Override
	public Pagination<SysUser> queryUserByPagination(String policenum, String name, String deptid, String checkResult,
			int pageIndex, String manageDept) {
		return sysUserDao.queryUserByPagination(policenum, name, deptid, checkResult, pageIndex, manageDept);
	}

	@Override
	public Pagination<SysUser> queryUserInfoByPagination(String name, String deptname, String tel_mobile, int pageIndex) {
		return sysUserDao.queryUserInfoByPagination(name, deptname, tel_mobile, pageIndex);
	}

	@Override
	public List<String[]> findRnameIdByUserApp(String policeNum, String appId) {
		return sysUserDao.findRnameIdByUserApp(policeNum, appId);
	}

	@Override
	public Pagination<SysUser> findUserContact(String deptId, int pageIndex, SysUser user, String inDept) {
		return sysUserDao.findUserContact(deptId, pageIndex, user, inDept);
	}

	@Override
	public Pagination<SysUser> findUserByDept(String deptId, int pageIndex) {
		return sysUserDao.findUserByDept(deptId, pageIndex);
	}

	@Override
	public Pagination<SysUser> findUserByCondition(Map<String, String> params, int pageIndex) {
		return sysUserDao.findUserByCondition(params, pageIndex);
	}

	public List<SysRole> findRoles(String policeNum) {
		return null;
	}

	@Override
	public SysUser login(String policeNum, String password) {
		return sysUserDao.findUserByPwdName(policeNum, password, 1);
	}

	@Override
	public List<Object> findRoleAndGroups(String policeNum) {
		return null;
	}

	@Override
	@Transactional
	public int saveInitRoles(String roles, String groups, String policeNum) {
		return 0;
	}

	@Override
	public void updateOrder(String policeNum, String type) {
		SysUser user = sysUserDao.findUserByPoliceNum(policeNum);
		String deptId = user.getDeptId();
		int orderNum = user.getOrderNum();
		int maxOrder = sysUserDao.findMaxOrderNum(deptId);
		int minOrder = sysUserDao.findMinOrderNum(deptId);
		if ("0".equals(type)) {
			// 排序移动到最顶端
			if (orderNum != minOrder)
				sysUserDao.updateOrderToTop(policeNum, deptId, minOrder, orderNum);
		} else if ("1".equals(type)) {
			// 排序上移
			boolean hasUp = true;
			if (minOrder == orderNum)
				hasUp = false;
			List<Integer> upDown = sysUserDao.findUpDownOrder(orderNum, deptId);
			sysUserDao.updateOrderUp(policeNum, deptId, upDown.get(0), orderNum, hasUp);
		} else {
			// 排序下移
			boolean hasDown = true;
			if (maxOrder == orderNum)
				hasDown = false;
			List<Integer> upDown = sysUserDao.findUpDownOrder(orderNum, deptId);
			sysUserDao.updateOrdeDown(policeNum, deptId, upDown.get(1), orderNum, hasDown);
		}
	}

	@Override
	public Pagination<SysUser> findManagerUsers(String deptId, int pageIndex, SysUser user, String manageDept) {
		return sysUserDao.findManagerUsers(deptId, pageIndex, user, manageDept);
	}

	@Override
	public void resetPwd(String policeNum, String password) {
		sysUserDao.resetPwd(policeNum, password);
	}

	@Override
	public List<SysUser> findUserByFuzzyName(String userName, int maxRow) {
		if (null != userName && StringUtils.hasText(userName.trim())) {
			return sysUserDao.findUserByFuzzyName(userName, maxRow);
		}
		return new ArrayList<SysUser>();
	}

	@Override
	public SysUser findUserByIdCard(String idCard) {
		return sysUserDao._findUserByIdCard(idCard);
	}

	@Override
	public void updateUserDept(Map<String, String> params, String deptAdmin,
			SysUser admin) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public List<SysUser> findUserByXm(String xm,String deptId) {
		return sysUserDao.findUserInfoByXm(xm,deptId);
	}

	@Override
	public List<SysUser> findAllUserByXm(String xm) {
		return sysUserDao.findAllUserInfoByXm(xm);
	}
}
